package dao

import (
	"context"
	"fmt"
	"mall-portalv2/internal/model"
)

func (d *Dao) QueryOmsOrderItemWhereOrderId(ctx context.Context, id int64) ([]model.OmsOrderItem, error) {
	tb := model.OmsOrderItem{}
	result := []model.OmsOrderItem{}

	if err := d.db.WithContext(ctx).Table(tb.TableName()).Where("order_id = ?", id).Find(&result).Error; err != nil {
		return result, err
	}
	return result, nil
}

func (d *Dao) QueryOmsOrderItemWhereOrderIds(ctx context.Context, ids []int64) ([]model.OmsOrderItem, error) {
	tb := model.OmsOrderItem{}
	result := []model.OmsOrderItem{}

	if err := d.db.WithContext(ctx).Table(tb.TableName()).Where("order_id in ?", ids).Find(&result).Error; err != nil {
		return result, err
	}
	return result, nil
}

func (d *Dao) QueryOmsItemAndProductWhereOrderId(ctx context.Context, id int64) ([]model.OmsOrderItemAndProduct, error) {
	tb1 := model.OmsOrderItem{}
	tb2 := model.PmsProduct{}
	result := []model.OmsOrderItemAndProduct{}

	if err := d.db.WithContext(ctx).Table(fmt.Sprintf("%s a", tb1.TableName())).Joins(fmt.Sprintf("LEFT JOIN %s b ON a.product_id = b.id", tb2.TableName())).Where("a.order_id = ?", id).Scan(&result).Error; err != nil {
		return result, err
	}
	return result, nil
}

func (d *Dao) QueryOmsItemAndProductInOrderIds(ctx context.Context, ids []int64) ([]model.OmsOrderItemAndProduct, error) {
	tb1 := model.OmsOrderItem{}
	tb2 := model.PmsProduct{}
	result := []model.OmsOrderItemAndProduct{}

	if err := d.db.WithContext(ctx).Table(fmt.Sprintf("%s a", tb1.TableName())).Joins(fmt.Sprintf("LEFT JOIN %s b ON a.product_id = b.id", tb2.TableName())).Where("a.order_id in ?", ids).Scan(&result).Error; err != nil {
		return result, err
	}
	return result, nil
}

func (d *Dao) QueryOmsOrderItemWhereOrderIdAndMemberID(ctx context.Context, id, MemberID int64) ([]model.OmsOrderItem, error) {
	tb := model.OmsOrderItem{}
	result := []model.OmsOrderItem{}

	if err := d.db.WithContext(ctx).Table(tb.TableName()).Where("order_id = ?", id, MemberID).Find(&result).Error; err != nil {
		return result, err
	}
	return result, nil
}

func (d *Dao) CreateOrderItem(ctx context.Context, insertData model.OmsOrderItem) (model.OmsOrderItem, error) {
	tb := model.OmsOrderItem{}
	if err := d.db.WithContext(ctx).Table(tb.TableName()).Create(&insertData).Error; err != nil {
		return tb, err
	}
	return insertData, nil
}

func (d *Dao) QueryOmsOrderItemCountWhereUserIdAndProductId(ctx context.Context, PromotionSkuId int64, memberId int64) ([]model.OmsOrderItemAndOrder, error) {
	tb1 := model.OmsOrderItem{}
	tb2 := model.OmsOrder{}
	result := []model.OmsOrderItemAndOrder{}
	//if err := d.db.WithContext(ctx).Table(tb.TableName()).Where("promotion_sku_id = ? AND member_id = ?", PromotionSkuId, memberId).Find(&result).Error; err != nil {
	//	return result, err
	//}

	if err := d.db.WithContext(ctx).Table(fmt.Sprintf("%s a", tb1.TableName())).Joins(fmt.Sprintf("LEFT JOIN %s b ON a.order_id = b.id", tb2.TableName())).Where("a.promotion_sku_id = ? AND a.member_id = ? AND b.delete_status != 1 AND b.status not in (4, 5)", PromotionSkuId, memberId).Scan(&result).Error; err != nil {
		return result, err
	}
	return result, nil
}
